<?php

/**
 * 前台 活动图库 模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: anli.class.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class image extends Model
{

    private $article_table;
    private $category_table;
    private $addonarticle_table;
    private $addonimages_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->article_table = '#@__article';
        $this->category_table = '#@__category';
        $this->addonarticle_table = '#@__addonarticle';
        $this->addonimages_table = '#@__addonimages';
    }


    /**
     * 获取所有资讯
     * @param string $url
     * @param string $type
     * @return <type>
     */
    public function getImageList($url = null, $cat_id)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->article_table WHERE type='post' AND cat_id = '$cat_id'");
        if ($url == '')
            $url = PHP_SELF . '?m=image&cid=' . $cat_id . '&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(12);
        $limit = $pages->getSqlLimit();
        $sql = "SELECT a.article_id , a.title, a.time, a.attachments, a.click_count, b.shijian, b.address FROM $this->article_table a LEFT JOIN $this->addonimages_table b ON a.article_id = b.article_id WHERE a.type='post' AND a.cat_id = '$cat_id' ORDER BY a.orderby ASC, a.article_id DESC LIMIT $limit";        
        $rs = $this->db->getAll($sql);        
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['time'] = date('Y-m-d', $v['time']);
            }
        }

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯! ";
        }

        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getImageInfo($aid)
    {
        $sql = "SELECT a.*, b.imgurls, b.content FROM $this->article_table a LEFT JOIN $this->addonimages_table b ON a.article_id = b.article_id WHERE a.article_id = '$aid' LIMIT 0,1";

        $rs = $this->db->getRow($sql);
        return $rs;
    }

    /**
     * 取案例文章列表
     * @param <type> $limit
     * @return <type>
     */
    public function getAnliList($limit=1)
    {
        $sql = "SELECT * FROM $this->article_table WHERE channel = '2' LIMIT 0,{$limit}";
        $rs = $this->db->getAll($sql);
        return $rs;
    }

    //取出configbody字段里的值
    public function getCfgBody($value, $configbody)
    {
        preg_match_all('/{' . $value . ' key="(.*)"}(.*){\/' . $value . '}/isU', $configbody, $result);
        return $result;
    }
}